import { DefStringProtocol, NetData } from "../network/NetInterface";
import { NetManager } from "../network/NetManager";
import { NetNode } from "../network/NetNode";
import { WebSock } from "../network/WebSock";
import { LogMgr } from "./LogMgr";

export class WsMgr {
    private static instance: WsMgr;
    private constructor() { }
    public static getInstance(): WsMgr {
        if (this.instance == null) {
            this.instance = new WsMgr();
        }

        return this.instance;
    }
    /**
     * 
     * @param url 连接地址 eg: wws:loaclhost:9090
     * @param platform 当前的平台id 没有 填写0
     * @param uid 用户id
     */
    public startSocket(url: string, platform: string, uid: string) {
        let Node = new NetNode();
        Node.init(new WebSock(), new DefStringProtocol(), null);
        /**接收信息 */
        Node.setResponeHandler(0, this.receiveMsg);
        NetManager.getInstance().setNetNode(Node);
        NetManager.getInstance().connect({ url: `${url}${platform}/${uid}`, autoReconnect: -1 });
    }
    /**接收到消息 */
    public receiveMsg(cmd: number, data: NetData) {
        LogMgr.getInstance().debug("接收的消息：" + data)
    }
    /**
     * 发送消息
     * @param data 消息
     */
    public send(data: any) {
        NetManager.getInstance().send(data);
    }
    /**关闭连接 */
    public closeSocket() {
        NetManager.getInstance().close();
    }
}